package com.syp.hehe.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.syp.hehe.dao.ArticleMapper;
import com.syp.hehe.domain.ArticleStatusEnum;
import com.syp.hehe.domain.ArticleTypeEnum;
import com.syp.hehe.domain.pojo.ArticleSchema;
import com.syp.hehe.domain.vo.ArticleVo;
import com.syp.hehe.service.ArticleServiceI;
import com.syp.hehe.util.PicDiskAdapterUtil;
import com.syp.hehe.util.PropertiesUtil;
import com.syp.hehe.util.SessionInfo;

@Service("articleService")
public class ArticleServiceImpl implements ArticleServiceI {

	@Autowired
	private ArticleMapper articleMapper;

	@Override
	public ArticleVo getById(Long id) {
		ArticleSchema articleSchema = this.articleMapper.getById(id);
		ArticleVo article = new ArticleVo();
		article.setId(articleSchema.getId());
		article.setContent(articleSchema.getContent());
		article.setPic(null);
		article.setType(articleSchema.getType());
		article.setPublishtime(articleSchema.getPublishtime());
		article.setAuthorUsername(articleSchema.getAuthorUsername());
		return article;
	}

	@Override
	public void insert(ArticleVo article) {
		ArticleSchema articleSchema = new ArticleSchema();

		articleSchema.setContent(article.getContent().replace("\r\n", "<br/>"));
		if (article.getPic().getSize() == 0L) {
			articleSchema.setType(ArticleTypeEnum.DUAN.getCode());
		} else {
			articleSchema.setType(ArticleTypeEnum.PIC.getCode());

			DateFormat df = new SimpleDateFormat("yyyyMMdd");
			String strDateSubfix = df.format(Calendar.getInstance().getTime());

			StringBuilder sb = new StringBuilder();
			sb.append(PropertiesUtil.sysConfigMap.get("hehe.pic.path"));
			sb.append(strDateSubfix);
			sb.append("/");
			sb.append(UUID.randomUUID().toString());
			sb.append(".");
			sb.append(article.getPic().getContentType().split("/")[1]);
			articleSchema.setPicPath(sb.toString());
			PicDiskAdapterUtil.write(article.getPic(), articleSchema.getPicPath());
		}
		HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
		SessionInfo sessionInfo = (SessionInfo) session.getAttribute("sessionInfo");
		articleSchema.setAuthorUsername(sessionInfo.getUsername());
		articleSchema.setPublishtime(Calendar.getInstance().getTime());
		articleSchema.setStatus(ArticleStatusEnum.NORMAL.getStatus());

		this.articleMapper.insert(articleSchema);
	}

	@Override
	public List<ArticleVo> getPageList(Integer type, Integer pageNum, Integer pageSize) {
		List<ArticleVo> list = new ArrayList<ArticleVo>(pageSize);
		int beginIndex = (pageNum - 1) * pageSize;
		List<ArticleSchema> articleSchemaList = this.articleMapper.getPageList(type, beginIndex, pageSize);
		for (ArticleSchema item : articleSchemaList) {
			ArticleVo article = new ArticleVo();

			article.setId(item.getId());
			article.setContent(item.getContent());
			article.setPicPath(item.getPicPath());
			article.setType(item.getType());
			article.setPublishtime(item.getPublishtime());
			article.setAuthorUsername(item.getAuthorUsername());

			list.add(article);
		}
		return list;
	}

	@Override
	public byte[] getPicBytes(Long id) {
		byte[] bytes = null;
		ArticleSchema articleSchema = this.articleMapper.getById(id);
		if (articleSchema != null) {
			bytes = PicDiskAdapterUtil.read(articleSchema.getPicPath());
		}
		return bytes;

	}

}
